package com.cat.bitOperation;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-or/
 * @create 2025/11/1 17:55
 * @since JDK17
 */

public class Solution15 {
    public long maximumOr(int[] nums, int k) {
        long ans = 0;
        int n = nums.length, prefix = 0;
        int[] suf = new int[n];
        for (int i = n - 2; i >= 0; i--) {
            suf[i] = suf[i + 1] | nums[i + 1];
        }
        for (int i = 0; i < n; i++) {
            ans = Math.max(ans,  prefix | ((long)nums[i] << k) | suf[i]);
            prefix |= nums[i];
        }

        return ans;
    }
}
